package com.epfly.promo.base;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.core.type.classreading.CachingMetadataReaderFactory;
import org.springframework.core.type.classreading.MetadataReader;
import org.springframework.core.type.classreading.MetadataReaderFactory;
import org.springframework.util.ClassUtils;

import java.beans.Introspector;
import java.io.IOException;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

@Configuration
public class FeignBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
    private ResourceLoader resourceLoader;


    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {


            ResourcePatternResolver resolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
            MetadataReaderFactory metaReader = new CachingMetadataReaderFactory(resourceLoader);
        try {
            Resource[] resources = resolver.getResources("classpath*:com/epfly/ifood/iface/**/*.class");

            for (Resource r : resources) {
                MetadataReader reader = metaReader.getMetadataReader(r);
                String className = reader.getClassMetadata().getClassName();
                System.out.println(className);
                Class<?> iClass = ClassUtils.resolveClassName(className, ClassUtils.getDefaultClassLoader());
                String beanName = Introspector.decapitalize(iClass.getSimpleName()) + "Impl";
                if (beanFactory.containsBean(beanName)) {
                    continue;
                }

                Object o = Proxy.newProxyInstance(ClassUtils.getDefaultClassLoader(), new Class[]{iClass}, new InvocationHandler() {
                    @Override
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                        String name = method.getName();
                        Class<?>[] parameterTypes = method.getParameterTypes();
                        Object feignService = beanFactory.getBean("com.epfly.sdk.promo.activity.UserServiceFeign");
                        Method fMethod = feignService.getClass().getMethod(name, parameterTypes);
                        Object iObj = fMethod.invoke(feignService, args);

                        return iObj;
                    }
                });
                beanFactory.registerSingleton(beanName, o);

            }

        } catch (IOException e) {

        }


    }
}
